package com.base.po.cfsp_sdk;

import com.alibaba.fastjson.JSON;
import com.base.helper.WebHelper;
import com.base.po.cfsp.UserBalance;
import com.base.po.cfsp.UserDeduct;
import com.base.utils.Produce;
import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;

/**
 * CFSP访问接口
 * */
public class CFSPInfoUtil {

    /**
     * 查询用户余额
     *
     * @param mobile 手机号
     * */
    public static UserBalance userBalance(String mobile){
        UserBalance userBalance = null;

        try {
            String timestamp = URLEncoder.encode(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Produce.getNowDate()), "UTF-8");
            String requestParam = "accesskey=" + CFSPConstants.ACCESS_KEY + "&mobile=" + mobile + "&timestamp=" + timestamp;
            String signParam = CFSPConstants.REQUEST_POST + "\n" + CFSPConstants.REQUEST_DOMAIN_NAME + "\n"
                    + CFSPConstants.REQUEST_METHOD_BALANCE + "\n" + requestParam;
            String sign = URLEncoder.encode(getSign(signParam, CFSPConstants.SECRET_KEY), "UTF-8");
            String url = CFSPConstants.USER_BALANCE + "?" + requestParam + "&sign=" + sign;
            String result = WebHelper.PostData(url,null,null,null);
            userBalance = JSON.toJavaObject(JSON.parseObject(result), UserBalance.class);
        }catch (Exception e){
            e.printStackTrace();
        }

        return userBalance;
    }

    /**
     * 用户扣币
     *
     * @param mobile 手机号
     * */
    public static UserDeduct userDeduct(String mobile, Double amount, String note){
        UserDeduct userDeduct = null;

        try {
            note =  URLEncoder.encode(note, "UTF-8");
            String timestamp = URLEncoder.encode(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Produce.getNowDate()), "UTF-8");
            String requestParam = "accesskey=" + CFSPConstants.ACCESS_KEY + "&amount=" + amount + "&mobile=" + mobile + "&note=" + note + "&timestamp=" + timestamp;
            String signParam = CFSPConstants.REQUEST_POST + "\n" + CFSPConstants.REQUEST_DOMAIN_NAME + "\n"
                    + CFSPConstants.REQUEST_METHOD_DEDUCT + "\n" + requestParam;
            String sign = URLEncoder.encode(getSign(signParam, CFSPConstants.SECRET_KEY), "UTF-8");
            String url = CFSPConstants.USER_DEDUCT + "?" + requestParam + "&sign=" + sign;
            String json = "{\"amount\":\"" + amount + "\",\"note\":\"" + note + "\"}";
            String result = WebHelper.doBodyPost(url, JSONObject.fromObject(json));
            userDeduct = JSON.toJavaObject(JSON.parseObject(result), UserDeduct.class);
        }catch (Exception e){
            e.printStackTrace();
        }

        return userDeduct;
    }

    private static String getSign(String params, String secretKey) {
        String hash = "";
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secret_key);
            byte[] bytes = sha256_HMAC.doFinal(params.getBytes());
            hash = Base64.encodeBase64String(bytes);
        } catch (Exception e) {
            System.out.println("Error HmacSHA256 ===========" + e.getMessage());
        }
        return hash;
    }
}
